import { useState } from 'react'
import { dynamicPageData, getPageInfo, savePageInfo } from '@/services/ActivityEditor'

interface PageInfo {
  pageName: string
  backgroundColor: string
  topImage: string
  couponList: any[]
  isHideCoupon: number
  goodsList: any[]
}

export default () => {
  const [couponList, setCouponList] = useState([]) // 优惠券列表
  const [goodsList, setGoodsList] = useState([]) // 商品列表
  const [isHideCoupon, setIsHideCoupon] = useState(0) // 是否隐藏优惠券
  const [currentTab, setCurrentTab] = useState(0) // 当前tab

  // 页面设置数据
  const [pageSettingData, setPageSettingData] = useState({
    pageName: '活动专题页',
    backgroundColor: 'default'
  })

  // 顶部图片数据
  const [topImageData, setTopImageData] = useState({
    topImage: 'https://qny.weizulin.cn/images/202404161048493.png'
  })

  // 动态计算页面优惠券和商品数据
  const DynamicPageData = (data: any) => {
    return dynamicPageData(data)
  }

  // 获取活动专题页详情接口
  const GetPageInfo = async (data: any) => {
    const res = await getPageInfo(data)

    const newRes = res as unknown as PageInfo

    // console.log('newRes========', newRes)

    if (res) {
      setPageSettingData({
        pageName: newRes.pageName,
        backgroundColor: newRes.backgroundColor
      })

      setTopImageData({
        topImage: newRes.topImage
      })

      setIsHideCoupon(newRes.isHideCoupon)

      setCouponList(newRes.couponList)

      setGoodsList(newRes.goodsList)
    }
  }

  // 保存活动专题页
  const SavePageInfo = (data: any) => {
    return savePageInfo(data)
  }

  return {
    // 优惠券列表
    couponList,
    setCouponList,

    // 是否隐藏优惠券
    isHideCoupon,
    setIsHideCoupon,

    // 商品列表
    goodsList,
    setGoodsList,

    // 页面设置数据
    pageSettingData,
    setPageSettingData,

    // 顶部图片数据
    topImageData,
    setTopImageData,

    // 当前tab
    currentTab,
    setCurrentTab,

    // 接口
    DynamicPageData,
    SavePageInfo,
    GetPageInfo
  }
}
